package com.gxa.eshops.portal.advice;

import com.gxa.eshps.cores.common.JsonResult;
import com.gxa.eshps.cores.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.ConstraintViolationException;
/**
 * @program: IntelliJ IDEA
 * @description:
 * @author: Mr.Wang
 * @create: 2022-06-29 10:45
 **/
@ControllerAdvice
@ResponseBody
@Slf4j
public class GloablExceptionHandler {


    /**
     * 处理绑定异常
     * @param errors 错误
     * @return {@code JsonResult}
     */
    @ExceptionHandler({BindException.class, MethodArgumentNotValidException.class})
    public JsonResult handleBindException(Errors errors) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        StringBuilder stringBuilder = new StringBuilder();
        errors.getAllErrors().forEach(error -> stringBuilder.append(error.getDefaultMessage()).append(","));
        jsonResult.setMsg(stringBuilder.toString());
        return jsonResult;
    }


    /**
     * 处理约束违反异常
     * @param exception 异常
     * @return {@code JsonResult}
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public JsonResult handleConstraintViolationException(ConstraintViolationException exception) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg(exception.getMessage());
        return jsonResult;
    }

    /**
     * 处理业务异常
     * @param exception 异常
     * @return {@code JsonResult}
     */
    @ExceptionHandler(BusinessException.class)
    public JsonResult handleBusinessException(BusinessException exception) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg(exception.getMessage());
        return jsonResult;
    }

    /**
     * 处理异常
     * @param e e
     * @return {@code JsonResult}
     */
    @ExceptionHandler(Exception.class)
    public JsonResult handleException(Exception e) {
        // 把系统异常日志记录
        log.info("-----------------begin-----------------------");
        log.info("Cause:"+ e.getCause());
        log.info("异常原因:"+e.getMessage());
        log.info(String.valueOf(e));
        // 先记录异常对象的toString状态: 包括异常类型和异常原因
        // 再遍历栈中的异常轨迹: 采用遍历方式
        StackTraceElement[] stackTrace = e.getStackTrace();
        for (StackTraceElement stackTraceElement : stackTrace) {
            log.info(String.valueOf(stackTraceElement));
        }
        log.info("-------------------end--------------------- ");
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("500");
        jsonResult.setMsg("服务器开小差了,请稍后再试");
        return jsonResult;
    }

}
